package com.mycase.im.boot.websocket.pattern.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycase.im.boot.entity.communication.ClientMessage;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import com.mycase.im.boot.entity.database.ImFriendRequest;
import com.mycase.im.boot.entity.database.ImUserFriend;
import com.mycase.im.boot.service.CircleService;
import com.mycase.im.boot.service.FriendRequestService;
import com.mycase.im.boot.service.UserFriendService;
import com.mycase.im.boot.websocket.WebSocketSignal;
import com.mycase.im.boot.websocket.pattern.MessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.Map;

/**
 * 用来处理好友申请的回复
 * @Author ZhuHJay
 * @Date 2021/9/18 21:14
 */
@Slf4j
@Service
public class ApplyForReply implements MessageStrategy {

    @Autowired
    private FriendRequestService friendRequestService;
    @Autowired
    private UserFriendService userFriendService;
    @Autowired
    private CircleService circleService;

    @Override
    public Boolean isUse(int type) {
        return WebSocketSignal.MESSAGE_REPLY_APPLY == type;
    }

    @Override
    public void executeHandler(ClientMessage clientMessage,
                               ObjectMapper objectMapper,
                               Map<String, Session> clientMaps,
                               Session mySession) throws Exception {
        log.warn("onMessage -匹配到-> ApplyForReply(申请回复)");

        // 根据id获取到对应的表信息
        ImFriendRequest imFriendRequest = friendRequestService.getById(clientMessage.getId());
        // 判断状态码: 1: 通过 -> 将好友信息传送, 添加一条推送的聊天记录 2: 拒绝 -> 申请方修改状态码
        ResponseMessage message;
        if( imFriendRequest.getStatus() == 1 ){
            // 获取到好友信息(该有的都得有)
            // 这是回复申请的人操作的, 他要发送一条信息给申请人, 告知他'我'同意了申请, 消息未读是申请人, 所以顺序不能乱
            ImUserFriend respFriend =
                    userFriendService.getFriendInfoByReplyApply(imFriendRequest.getRespId(), imFriendRequest.getReqId());
            // 获取它的动态消息
            respFriend.setDynamics(circleService.findDynamicListByUserId(respFriend.getImUser()));
            // 这是申请人操作的, 获取好友的信息
            ImUserFriend reqFriend =
                    userFriendService.getFriendInfoByApply(imFriendRequest.getReqId(), imFriendRequest.getRespId());
            // 获取它的动态消息
            reqFriend.setDynamics(circleService.findDynamicListByUserId(reqFriend.getImUser()));
            // 将聊天记录放一份给申请人
            reqFriend.setChatRecord(respFriend.getChatRecord());
            // 修改申请人的未读聊天记录条数
            reqFriend.setUnReadCount(1);

            message = ResponseMessage.success()
                            .appendData("reqFriend", reqFriend)
                            .appendData("respFriend", respFriend)
                            .appendData("type", WebSocketSignal.MESSAGE_REPLY_APPLY)
                            .appendData("imFriendRequest", imFriendRequest);
        }else{
            message = ResponseMessage.success()
                            .appendData("type", WebSocketSignal.MESSAGE_REPLY_APPLY)
                            .appendData("imFriendRequest", imFriendRequest);
        }
        // 发送消息
        sendMessage(objectMapper, mySession, message);
        if( clientMaps.containsKey(imFriendRequest.getReqId()) ){
            sendMessage(objectMapper, clientMaps.get(imFriendRequest.getReqId()), message);
        }
    }
}
